package com.ruoyi.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.manage.domain.SysMechanism;
import com.ruoyi.manage.mapper.MechanismManageMapper;
import com.ruoyi.manage.service.MechanismManageService;
import com.ruoyi.manage.threadLocal.UserThreadLocal;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * @author 满杯百香果🍎
 */
@Service
public class MechanismManageServiceImpl extends ServiceImpl<MechanismManageMapper, SysMechanism> implements MechanismManageService {

    @Autowired
    private MechanismManageMapper mapper;
    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    @Override
    public List<SysMechanism> getList() {
        // 传入用户名，查询相关权限
        String username = UserThreadLocal.getUsername();
        if (username.equals("admin")) {
            return mapper.getList();
        }
        SysMechanism sysMechanism = mapper.getOne(username);
        List<SysMechanism> children = getChildrenByParentId(sysMechanism.getId());
        sysMechanism.setChildren(children);
        children = new ArrayList<>();
        children.add(sysMechanism);
        return children;
    }

//    @Override
//    public List<SysMechanism> getUnderMechanismList(Long id) {
//        return mapper.getUnderMechanismList(id);
//
//    }

    @Override
    public List<SysMechanism> getChildrenByParentId(Long id) {

        return mapper.getChildrenByParentId(id);
    }

    @Override
    public List<SysMechanism> getListByMechanismCodeAndMechanismName(SysMechanism sysMechanism) {
        // 传入用户名，查询相关权限
        String username = UserThreadLocal.getUsername();
        if (username.equals("admin")) {
            return mapper.getListByParam(sysMechanism);
        }

        // 自己的机构
        SysMechanism mechanism = mapper.getOne(username);
        if (
                Strings.isEmpty(sysMechanism.getMechanismCode()) && Strings.isEmpty(sysMechanism.getMechanismName())
                        || (mechanism.getMechanismCode().equals(sysMechanism.getMechanismCode()) || mechanism.getMechanismName().equals(sysMechanism.getMechanismName()))

        ) {
            return getList();
        }
        // 要查询的机构
        List<SysMechanism> list = mapper.getOneByMechanismCodeAndMechanismName(sysMechanism.getMechanismCode(), sysMechanism.getMechanismName(), mechanism.getLevelId(), mechanism.getId());
        List<SysMechanism> list2 = new ArrayList<>();
        if (list != null)
            for (SysMechanism mechanism2 : list) {
                if ((mechanism.getMechanismCode().equals(mechanism2.getMechanismCode()) || mechanism.getLevelId() < mechanism2.getLevelId())) {
                    List<SysMechanism> children = getChildrenByParentId(mechanism2.getId());
                    mechanism2.setChildren(children);
                    list2.add(mechanism2);
                }
            }

        return list2;
    }

    @Override
    public AjaxResult addMechanism(SysMechanism sysMechanism) {
        if (!Strings.isEmpty(sysMechanism.getParentMechanismName())){
            SysMechanism parent = mapper.getMechanismByName(sysMechanism.getParentMechanismName());
            if (parent == null){
                return AjaxResult.error("此父机构不存在！");
            }
            if (sysMechanism.getLevelId() <= parent.getLevelId()) {
                return AjaxResult.error("新增的机构级别不得大于或等于上级机构！");
            }
            sysMechanism.setParentId(parent.getId());
        }
        if (mapper.addMechanism(sysMechanism) != 0) {
            return AjaxResult.success("新增成功！");
        }
        return AjaxResult.error("新增的未知错误！");
    }

    @Override
    public Integer delMechanism(Long id) {
        Integer count = mapper.hasChildren(id);
        if (count == 0) {
            return mapper.deleteById(id);
        }
        return 0;
    }


}
